﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

/// <summary>
/// TextOperation 的摘要说明
/// 文书操作模块
/// </summary>
public class TextOperation
{
    private Dbcom cm;
    string sql = "select * from t_text t1 where t1.enable_flag='Y' order by shownum";
    DataSet dsall = new DataSet();

    public TextOperation(Dbcom CM)
    {
        //
        // TODO: 在此处添加构造函数逻辑
        // 
        cm = CM;     
    }

    /// <summary>
    /// 实例Class_Text化查询结果
    /// </summary>
    /// <param Name="tempds"></param>
    /// <returns></returns>
    public Class_Text[] GetSelectClassDs(DataSet tempds)
    {
        if (tempds != null)
        {
            if (tempds.Tables[0].Rows.Count > 0)
            {
                Class_Text[] class_text = new Class_Text[tempds.Tables[0].Rows.Count];
                for (int i = 0; i < tempds.Tables[0].Rows.Count; i++)
                {
                    class_text[i] = new Class_Text();
                    class_text[i].Id = Convert.ToInt32(tempds.Tables[0].Rows[i]["ID"].ToString());
                    if (tempds.Tables[0].Rows[i]["PARENTID"] != null)
                    {
                        if (tempds.Tables[0].Rows[i]["PARENTID"].ToString() != "0" && tempds.Tables[0].Rows[i]["PARENTID"].ToString() != "")
                        {
                            class_text[i].Parentid = Convert.ToInt32(tempds.Tables[0].Rows[i]["PARENTID"].ToString());
                        }
                    }
                    class_text[i].Textcode = tempds.Tables[0].Rows[i]["TEXTCODE"].ToString(); ;
                  
                    class_text[i].Textname = tempds.Tables[0].Rows[i]["TEXTNAME"].ToString();                   
                    class_text[i].Isenable = tempds.Tables[0].Rows[i]["ISENABLE"].ToString();
                    class_text[i].Txxttype = tempds.Tables[0].Rows[i]["ISBELONGTOTYPE"].ToString();
                   
                    class_text[i].Issimpleinstance = tempds.Tables[0].Rows[i]["ISSIMPLEINSTANCE"].ToString();
                    class_text[i].Enable = tempds.Tables[0].Rows[i]["ENABLE_FLAG"].ToString();
                    class_text[i].Shownum = tempds.Tables[0].Rows[i]["shownum"].ToString();
                    class_text[i].Ishighersign = tempds.Tables[0].Rows[i]["ishighersign"].ToString();
                    class_text[i].Ishavetime = tempds.Tables[0].Rows[i]["ishavetime"].ToString();
                    class_text[i].Formname = tempds.Tables[0].Rows[i]["formname"].ToString();
                    class_text[i].Other_textname = tempds.Tables[0].Rows[i]["OTHER_TEXTNAME"].ToString();
                    if (tempds.Tables[0].Rows[i]["RIGHT_RANGE"].ToString() == "")
                    {
                        class_text[i].Right_range = "D";
                    }
                    else
                    {
                        class_text[i].Right_range = tempds.Tables[0].Rows[i]["RIGHT_RANGE"].ToString();
                    }

                    if (tempds.Tables[0].Rows[i]["ISNEEDSIGN"].ToString() == "")
                    {
                        class_text[i].Isneedsign = "Y";
                    }
                    else
                    {
                        class_text[i].Isneedsign = tempds.Tables[0].Rows[i]["ISNEEDSIGN"].ToString();
                    }

                    if (tempds.Tables[0].Rows[i]["ISNEWPAGE"].ToString() == "")
                    {
                        class_text[i].Isnewpage = "N";
                    }
                    else
                    {
                        class_text[i].Isnewpage = tempds.Tables[0].Rows[i]["ISNEWPAGE"].ToString();
                    }

                    if (tempds.Tables[0].Rows[i]["ISSUBMITSIGN"].ToString() == "")
                    {
                        class_text[i].Issubmitsign = "N";
                    }
                    else
                    {
                        class_text[i].Issubmitsign = tempds.Tables[0].Rows[i]["ISSUBMITSIGN"].ToString();
                    }

                }
                return class_text;
            }
            else
            {
                return null;
            }
        }
        else
        {
            return null;
        }
    }

    /// <summary>
    /// 获取全部分文书
    /// </summary>
    /// <returns></returns>
    public List<UcTreeNode> GetAllText()
    {
        try
        {
            string sqlptext = "select * from t_text t1 where t1.parentid=0 order by t1.shownum asc";           
            dsall = Dbcom.GetDataSet(sql); //获取所有文书                      
            DataSet ds = Dbcom.GetDataSet(sqlptext); //顶级节点文书

            List<UcTreeNode> nodes = new List<UcTreeNode>();
            //所有顶级节点
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                UcTreeNode tn = new UcTreeNode();
                Class_Text text = new Class_Text();

                text.Id = Convert.ToInt32(ds.Tables[0].Rows[i]["id"].ToString());
                text.Textcode = ds.Tables[0].Rows[i]["textcode"].ToString();
                text.Textname = ds.Tables[0].Rows[i]["textname"].ToString();
                text.Parentid = Convert.ToInt32(ds.Tables[0].Rows[i]["parentid"].ToString());
                text.Issimpleinstance = ds.Tables[0].Rows[i]["issimpleinstance"].ToString();
                text.Shownum = ds.Tables[0].Rows[i]["shownum"].ToString();


                tn.Name = text.Id.ToString();
                tn.Text = text.Textname;
                tn.Tag = text; //对象绑定

                tn.Childnodes = new List<UcTreeNode>();
                GetChildList(ref tn, dsall);
                nodes.Add(tn);
            }
            return nodes;
        }
        catch
        {
            return null;
        }
    }



    /// <summary>
    ///  显示文书
    /// </summary>
    /// <param inpo="trvBook"></param>
    public UcTreeNode ReflashBookTree(InPatientInfo inpo)
    {
        try
        {
            UcTreeNode trvBook = new UcTreeNode();         
            trvBook.Childnodes.Clear();
            //查出所有文书
            string SQl = "select * from T_TEXT where enable_flag='Y' and right_range in ('D','A') and (sid='0' or instr(sid,'" +
                inpo.Section_Id.ToString() + ",')=1 or instr(sid,'," +
                inpo.Section_Id.ToString() + ",')>0) order by shownum asc";
            //找出文书所有类别
            //string Sql_Category = "select * from t_data_code where type=31 and enable='Y'";
            DataSet ds = new DataSet();
            ds = Dbcom.GetDataSet(SQl);
            Class_Text[] Directionarys = GetSelectClassDs(ds);
            //得到文书的类型
            //DataSet ds_category = Dbcom.GetDataSet(Sql_Category);
            //Class_Datacodecs[] datacodes = GetSelectDirectionary(ds_category);           
            if (Directionarys != null)
            {
                for (int i = 0; i < Directionarys.Length; i++)
                {
                    if (Directionarys[i].Parentid == 0)
                    {
                        UcTreeNode tn = new UcTreeNode();
                        tn.Tag = Directionarys[i];
                        tn.Text = Directionarys[i].Textname;
                        tn.Name = Directionarys[i].Id.ToString();

                        SetTreeView(Directionarys, tn);   //插入文书的子类文书。                                                                 
                        trvBook.Childnodes.Add(tn);
                    }
                }                
            }            
            return trvBook;
        }
        catch
        {
            return null;
        }
    }

    /// <summary>
    /// 根据文书的ID获取文书的实体类
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public Class_Text GetTextClsById(string id)
    {
        DataSet ds = Dbcom.GetDataSet("select * from T_TEXT  where ID=" + id + "");
        if (ds != null)
        {
            Class_Text[] temps = GetSelectClassDs(ds);
            if (temps != null)
            {
                if (temps.Length > 0)
                {
                    return temps[0];
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;
            }
        }
        else
        {
            return null;
        }
    }

    /// <summary>
    /// 根据名称模糊查询文书类型集合
    /// </summary>
    /// <param name="textname"></param>
    /// <returns></returns>
    public Class_Text[] GetClassTestDsByName(string textname)
    {
      
        string SQl = "select * from T_TEXT where TEXTNAME like '%" + textname + "%' order by shownum asc";       
        DataSet ds = new DataSet();
        ds = Dbcom.GetDataSet(SQl);
        Class_Text[] Directionarys = GetSelectClassDs(ds);
        return Directionarys;
    }

    /// <summary>
    /// 更改文书节点的排序
    /// </summary>
    /// <param name="shownum1"></param>
    /// <param name="id1"></param>
    /// <param name="shownum2"></param>
    /// <param name="id2"></param>
    /// <returns></returns>
    public int ChangeTextShowNum(string shownum1,string id1,string shownum2,string id2)
    {
        string[] sqls = new string[2];
        sqls[0] = "update t_text set shownum=" + shownum1 + " where id='" + id2 + "' and idnum<>'" + shownum1 + "'";
        sqls[1] = "update t_text set shownum=" + shownum2 + " where id='" + id1 + "' and idnum<>'" + shownum1 + "'";
        return Dbcom.ExecuteBatch(sqls);
    }

    /// <summary>
    /// 添加文书
    /// </summary>
    /// <returns></returns>
    public int InsertText(string txtName,string txtNumber,string cboEditorSelectIndex,string fahterId,
        string cboBookAttributeSelectedIndex,string mark,string sid,string signatrue,string cboTimeTitle,
        string fromname,string cboDoctorOrNuser,string isnewpage,string issubmitsign,string isneedsignatrue,
        string txtOtherTextName)
    {
       string  Sql = "insert into T_TEXT(TEXTNAME,TEXTCODE,ISENABLE,PARENTID,ISSIMPLEINSTANCE,ENABLE_FLAG,SID,ISHIGHERSIGN,ISHAVETIME,FORMNAME,RIGHT_RANGE,ISNEWPAGE,ISSUBMITSIGN,ISNEEDSIGN,OTHER_TEXTNAME)values('"
                         + txtName + "','"
                         + txtNumber + "',"
                         + cboEditorSelectIndex + ","
                         + fahterId + ","
                         + cboBookAttributeSelectedIndex + ",'"
                         + mark + "','"
                         + sid + "','"
                         + signatrue + "','"
                         + cboTimeTitle + "','" + fromname + "','" + cboDoctorOrNuser + "','" + isnewpage + "','" + issubmitsign + "','" + isneedsignatrue + "','" + txtOtherTextName + "')";
        return Dbcom.ExecuteSQL(Sql);
    }

    /// <summary>
    /// 修改文书
    /// </summary>
    /// <returns></returns>
    public int UpdateText(string txtName,string txtNumber,string cboEditorSelectedIndex,string fahterId,string cboBookAttributeSelectedIndex,
                         string mark,string sid,string signatrue,string cboTimeTitle,string fromname,string cboDoctorOrNuser,
                         string isnewpage,string issubmitsign,string isneedsignatrue,string txtOtherTextName,string selectClasstextId)
    {
      string Sql = "update T_TEXT set  TEXTNAME='"
                      + txtName + "',TEXTCODE='"
                      + txtNumber + "',ISENABLE='"
                      + cboEditorSelectedIndex + "',PARENTID='"
                      + fahterId + "',ISSIMPLEINSTANCE='" + cboBookAttributeSelectedIndex
                      + "',ENABLE_FLAG='" + mark
                      + "',SID='" + sid
                      + "', ishighersign='" + signatrue
                      + "',ishavetime='" + cboTimeTitle
                      + "',formname='" + fromname +
                      "',RIGHT_RANGE='" + cboDoctorOrNuser +
                      "',ISNEWPAGE='" + isnewpage + "',ISSUBMITSIGN='" + issubmitsign +
                      "',ISNEEDSIGN='" + isneedsignatrue +
                      "',OTHER_TEXTNAME='" + txtOtherTextName + "' where id='" + selectClasstextId + "'";
        return Dbcom.ExecuteSQL(Sql);
    }

    #region 私有函数
    /// <summary>
    /// 对文书进行分类设置
    /// </summary>
    /// <param Name="Directionarys">所有文书节点集合</param>
    /// <param Name="currentnode">当前文书节点</param>
    private void SetTreeView(Class_Text[] Directionarys, UcTreeNode current)
    {
        for (int i = 0; i < Directionarys.Length; i++)
        {
            Class_Text cunrrentDir = (Class_Text)current.Tag;
            if (Directionarys[i].Parentid == cunrrentDir.Id)
            {
                UcTreeNode tn = new UcTreeNode();
                tn.Tag = Directionarys[i];
                tn.Text = Directionarys[i].Textname;
                tn.Name = Directionarys[i].Id.ToString();
                current.Childnodes.Add(tn);
                SetTreeView(Directionarys, tn);
            }
        }
    }

    /// <summary>
    /// 实例化查询结果
    /// </summary>
    /// <param Name="tempds"></param>
    /// <returns></returns>
    private Class_Datacodecs[] GetSelectDirectionary(DataSet tempds)
    {
        if (tempds != null)
        {
            if (tempds.Tables[0].Rows.Count > 0)
            {
                Class_Datacodecs[] Directionary = new Class_Datacodecs[tempds.Tables[0].Rows.Count];
                for (int i = 0; i < tempds.Tables[0].Rows.Count; i++)
                {
                    Directionary[i] = new Class_Datacodecs();
                    Directionary[i].Id = tempds.Tables[0].Rows[i]["ID"].ToString();
                    Directionary[i].Name = tempds.Tables[0].Rows[i]["NAME"].ToString();
                    Directionary[i].Code = tempds.Tables[0].Rows[i]["CODE"].ToString();
                    Directionary[i].Shortchut_code = tempds.Tables[0].Rows[i]["SHORTCUT_CODE"].ToString();
                    Directionary[i].Enable = tempds.Tables[0].Rows[i]["ENABLE"].ToString();
                    Directionary[i].Type = tempds.Tables[0].Rows[i]["TYPE"].ToString();
                }
                return Directionary;
            }
            else
            {
                return null;
            }
        }
        else
        {
            return null;
        }
    }

    /// <summary>
    /// 递归算法
    /// </summary>
    /// <param name="node"></param>
    /// <param name="Datatexts"></param>
    private void GetChildList(ref UcTreeNode node, DataSet Datatexts)
    {
        for (int i = 0; i < Datatexts.Tables[0].Rows.Count; i++)
        {
            if (Datatexts.Tables[0].Rows[i]["parentid"].ToString() == node.Name)
            {
                UcTreeNode tn = new UcTreeNode();
                Class_Text text = new Class_Text();

                text.Id = Convert.ToInt32(Datatexts.Tables[0].Rows[i]["id"].ToString());
                text.Textcode = Datatexts.Tables[0].Rows[i]["textcode"].ToString();
                text.Textname = Datatexts.Tables[0].Rows[i]["textname"].ToString();
                text.Parentid = Convert.ToInt32(Datatexts.Tables[0].Rows[i]["parentid"].ToString());
                text.Issimpleinstance = Datatexts.Tables[0].Rows[i]["issimpleinstance"].ToString();
                text.Shownum = Datatexts.Tables[0].Rows[i]["shownum"].ToString();

                tn.Name = text.Id.ToString();
                tn.Text = text.Textname;
                tn.Tag = text; //对象绑定
                tn.Childnodes = new List<UcTreeNode>();
                GetChildList(ref tn, Datatexts);
                node.Childnodes.Add(tn);

            }
        }
    }
    #endregion
}
